package com.heima.manger;



import com.heima.domain.FansDetail;
import com.heima.domain.WeChatDate;
import com.heima.service.WeChatService;
import com.heima.utils.ConstantUtil;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;

@Component
public class WeChatManger {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private WeChatService weChatSerVice;

  /*  //通过微信openid查询是否有对应的id
    public Boolean findByOpenId(String openid, String nickeName) {
        KafkaProperties.Admin admin = weChatSerVice.findByOpenId(openid);
        if (admin == null) {
            //说明没有权限。调用中间件发送微信通知给管理员
            rocketMQTemplate.convertAndSend("wxMes", nickeName);

            return false;
    }
        return true;

    }*/

    //获取微信关注总人数
    public Integer getFans(){

    //获取token
        String token = stringRedisTemplate.opsForValue().get(ConstantUtil.ACCESS_TOKEN);
        String url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token="+token;
                //发送get请求
                RestTemplate restTemplate = new RestTemplate();
        WeChatDate weChatDate = restTemplate.getForObject(url, WeChatDate.class);
        System.out.println(weChatDate);
        Integer total = weChatDate.getTotal();//关注总人数
        WeChatDate.DataBean data = weChatDate.getData();
        List<String> openids = data.getOpenid();
        for (String openid : openids) {
            System.out.println(openid);
        }
        return total;


    }
    //获取粉丝的详情
    public FansDetail fansDetail(String openId) {
        //获取token
        String token = stringRedisTemplate.opsForValue().get(ConstantUtil.ACCESS_TOKEN);
        String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+token+"&openid="+openId;
        //发送get请求
        RestTemplate restTemplate = new RestTemplate();
        FansDetail forObject = restTemplate.getForObject(url, FansDetail.class);
        System.out.println(forObject);
        return forObject;

    }
}
